<?php
$member_slug = pods_url_variable('last');
$member_pod_item = new Pod('users',$member_slug);
$brand_fav = $member_pod_item->get_field('favorite_brands');
$users_name = $member_pod_item->get_field('name');
$member_pod_item_created = $member_pod_item->get_field('created');
$split_name = explode(' ',$users_name);


/* User Activity Feed */
$comments_name = $member_pod_item->get_field('comments.name');
if(!empty($comments_name) && !is_array($comments_name)){
  $comments_name = array($comments_name);
}

$comments_deal_name = $member_pod_item->get_field('comments.associated_deal.name');
if(!empty($comments_deal_name) && !is_array($comments_deal_name)){
  $comments_deal_name = array($comments_deal_name);
}

$deals_name = $member_pod_item->get_field('deals_added.name');
if(!empty($deals_name) && !is_array($deals_name)){
  $deals_name = array($deals_name); 
}

$camp_start = $member_pod_item->get_field('deals_added.camp_start');
if(!empty($camp_start) && !is_array($camp_start)){
  $camp_start = array($camp_start); 
}

$camp_link = $member_pod_item->get_field('deals_added.camp_link');
if(!empty($camp_link) && !is_array($camp_link)){
  $camp_link = array($camp_link); 
}

if(!empty($comments_name) && is_array($comments_name)){
  foreach($comments_name as $key=>$value){
    $comment_info = explode('_',$value);
    $deals_comments[$key]['strtotime'] = $comment_info['1'];
    $comment_time = date('Y-m-d H:i:s',$comment_info['1']);
    $deals_comments[$key]['date'] = date('Y-m-d H:i:s',(strtotime($comment_time."+ 5 hours 30 minutes")));
    //$deals_comments[$key]['deal_name'] = $comments_deal_name;//$comment_info['0'];
    //$deals_comments[$key]['deal_link'] = sanitize_title($deals_comments[$key]['deal_name']);
    $deals_comments[$key]['comment'] = 1; 
  }
}

if(!empty($comments_deal_name) && is_array($comments_deal_name)){
   foreach($comments_deal_name as $key=>$value){
    $deals_comments[$key]['deal_name'] = $value;
    $deals_comments[$key]['deal_link'] = $deals_comments[$key]['deal_name'];
   }
}

/* Array Manipulation for User Activity */
if(!empty($deals_name) && is_array($deals_name)){
    foreach($camp_start as $key_date => $value_date){
      	if(!empty($value_date) && !empty($key_date)){
		$deals_dates[$key_date]['strtotime'] = strtotime($value_date);
		$deals_dates[$key_date]['date'] = date('Y-m-d H:i:s',(strtotime($value_date."+ 5 hours 30 minutes")));
	}
    }

    foreach($deals_name as $key_name=>$value_name)
    {
      $deals_dates[$key_name]['deal_name'] = $value_name;
      $deals_dates[$key_name]['deal'] = 1; 
    }
    
    foreach($camp_link as $key_link=>$value_link)
    {
      $deals_dates[$key_link]['deal_link'] = $value_link; 
    }
}
  
  if((!empty($deals_name) && is_array($deals_name)) && (!empty($comments_name) && is_array($comments_name))){
    $final_deals = array_merge($deals_comments, $deals_dates);
    $sorted_activity = array_orderby($final_deals, 'strtotime', SORT_DESC);
  }
  elseif((!empty($deals_name) && count($deals_name) > 0)){
    $sorted_activity = array_orderby($deals_dates, 'strtotime', SORT_DESC);
  }
  elseif((!empty($comments_name) && count($comments_name) > 0)){
    $sorted_activity = array_orderby($deals_comments, 'strtotime', SORT_DESC);
  }

  $created_activity['created'] = $split_name['0'].' registered on <b>'.date('jS M, Y', strtotime($member_pod_item_created)).'</b>';
  
  if(is_array($sorted_activity)){
    array_push($sorted_activity,$created_activity); 
  }
  else{
    $sorted_activity = array($created_activity);
  }

  /* Array Manipulation for User Activity */
/* User Activity Feed */
//print_r($sorted_activity);
?>
<div class="brand_head">
<h1 id="f_margin"> </h1>

<div class="clear"></div>

  <div class="member_tabbed tabbed">
	<ul class="tabs">
		<li class="t1"><a class="t1 tab">Hand Picked Offers</a></li>
		<li class="t2"><a class="t2 tab">Offers Shared</a></li>
		<li class="t3"><a class="t3 tab">Member Activity</a></li>	
		<div class="clear"></div>	
	</ul>
 	
 	<?php if(!empty($brand_fav)){ ?>
  	<div class="t1" id="t1">
  	
  	<div class="views">
	  <a class="grid g-button">Grid View</a>
	  <a class="list g-button">List View</a>
	</div>
  	<div class="clear"></div>
	<div id="list_wrap">
		<ul class="list">
		<?php 
			  $count_rt = count($brand_fav);
			  $k = 0;
			  foreach($brand_fav as $value)
			  {
			    $brands_string.= "'".$value[id]."'";
					if($k != ($count_rt-1))
					{
						$brands_string.= ",";
					}
					$k++;
			  }
			  $brands_string = (string)$brands_string;               
			  
			  $campaigns = new Pod('deal');
			  $where = "t.camp_end > ".fix_date()." && brand.id IN(".$brands_string.") && daily='0'";   
				$campaigns->findRecords('RAND()', 10, $where);
				echo $campaigns->showTemplate('list_grid');
		?>	
		</ul>
	</div>
	<div class="clear"></div>
		<div>
			<?php echo $campaigns->getPagination(); ?>
		</div>	
	
	</div>
	<?php }	?>
	
	<div class="t2" id="t2">
		<div class="views">
		  <a class="grid g-button">Grid View</a>
		  <a class="list g-button">List View</a>
		</div>
		<div class="clear"></div>
		<div id="list_wrap">
			<ul class="list">
			<?php 
				$local_offers = new Pod('deal');
				$where = "added_by_user.slug='".$member_slug."'";   
				$local_offers->findRecords('id DESC', 20, $where);
				echo $local_offers->showTemplate('list_grid');
			?>
			</ul>
		</div>
		<div class="clear"></div>
		<div>
			<?php echo $local_offers->getPagination(); ?>
		</div>	
	</div>
	<div class="t3" id="t3">
	<?php
    foreach($sorted_activity as $key_sorted => $value_sorted){
      if($value_sorted['deal'] == '1'){
        echo '<div class="member_activity_deal"><div class="member_date">'.date('jS M, Y',strtotime($value_sorted['date'])).'</div><div class="member_activity_content">'.$split_name['0'].' shared an offer <a href="'.BUZZR_URL.'/deals/'.$value_sorted['deal_link'].'">'.$value_sorted['deal_name'].'</a> & gained 500 Pts.</div></div>';
      }
      elseif($value_sorted['comment'] == '1'){
        echo '<div class="member_activity_comment"><div class="member_date">'.date('jS M, Y',strtotime($value_sorted['date'])).'</div><div class="member_activity_content">'.$split_name['0'].' commented on <a href="'.BUZZR_URL.'/deals/'.$value_sorted['deal_link'].'">'.$value_sorted['deal_name'].'</a> & gained 50 Pts.</div></div>';
      }
      elseif(!empty($value_sorted['created'])){
        echo '<div class="member_activity_comment">'.$value_sorted['created'].'</div>';
      }
    }
	?>	
	</div>	
	</div>
</div>